package com.iot.inclinometer.util;


import com.google.gson.Gson;

public class JsonUtil{

//    private static ObjectMapper mapper = new ObjectMapper();
//
//    static {
//        // 解决实体未包含字段反序列化时抛出异常
//        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        // 对于空的对象转json的时候不抛出错误
//        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//        // 允许属性名称没有引号
//        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//        // 允许单引号
//        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//    }
//
//    public static String toJsonString(Object obj) throws JsonProcessingException {
//        return mapper.writeValueAsString(obj);
//    }
//
//    public static <T> T toClass(String json, Class<T> beanType) throws JsonProcessingException {
//        return mapper.readValue(json, beanType);
//    }


    public static String toJsonString(Object obj){

        Gson gson = new Gson();
        return gson.toJson(obj);
    }


    public static <T> T toClass(String json, Class<T> beanType){
        Gson gson = new Gson();
        return gson.fromJson(json,beanType);
    }
}